using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using QuoteTree;
public partial class ChangeTreeValue : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session.IsNewSession)
        {
            // If it says it is a new session, but an existing cookie exists, then it must 
            // have timed out (can't use the cookie collection because even on first 
            // request it already contains the cookie (request and response
            // seem to share the collection)
            string szCookieHeader = Request.Headers["Cookie"];
            if ((null != szCookieHeader) && (szCookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
            {
                Response.Redirect("sessionTimeout.aspx");
            }
        }

        this.TextBox1.Attributes.Add("onChange", "intOnly(this);");
        this.TextBox1.Attributes.Add("onKeyUp", "intOnly(this);");
        this.TextBox1.Attributes.Add("onKeyPress", "intOnly(this);");

        string[] splitted = this.Request.QueryString["node"].Split("\\".ToCharArray());

        this.Label2.Text = splitted[splitted.Length - 2] + ":";
        this.Label3.Text = splitted[splitted.Length - 1];
        if (this.Session["tree"] != null && this.Request.QueryString["node"] != null && this.Request.QueryString["node"].ToString() != "")
        {
            QTree t = this.Session["tree"] as QTree;
            string node = this.Request.QueryString["node"].ToString().Trim();
            string company = this.Session["company"].ToString();
            this.IFRAME2.Attributes.Add("src", "quotes/" + company + "/" + node.Replace("\\", "/") + "/homepage.htm");
            MathNode n = t.GetNodeFromPath(node) as MathNode;
            if (n.hidden) this.Page.Controls.Clear();
            decimal res;
            if (n.edit_children)
            {
                this.TextBox1.Visible = false;
                this.Label4.Visible = false;
                this.Panel1.Visible = true;
                //this.TD1.Controls.Remove(FindControl("TextBox1"));
                //this.TD1.Controls.Remove(FindControl("Label4"));

                foreach (INode child in n.children)
                {
                    if (child.type == NodeType.math && decimal.TryParse((child as MathNode).formula, out res) && !child.hidden)
                    {
                        Label l = new Label(); l.Text = child.name + " (" + child.units + ")"; l.Font.Size = FontUnit.XSmall; l.Font.Name = "arial";
                        LiteralControl lit = new LiteralControl("<BR>");
                        TextBox text = new TextBox(); text.ID = child.name; text.Font.Size = FontUnit.XSmall; text.Width = Unit.Pixel(40);
                        text.AutoCompleteType = AutoCompleteType.Disabled;
                        text.Attributes.Add("onChange", "intOnly(this);");
                        text.Attributes.Add("onKeyUp", "intOnly(this);");
                        text.Attributes.Add("onKeyPress", "intOnly(this);");
                        this.Panel1.Controls.Add(text);
                        this.Panel1.Controls.Add(new LiteralControl(" "));
                        this.Panel1.Controls.Add(l);
                        this.Panel1.Controls.Add(lit);
                    }
                }
            }
            else this.Label4.Text = n.units;

        }
        else this.Page.Controls.Clear();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        if (this.Session["tree"] != null && this.Request.QueryString["node"] != null && this.Request.QueryString["node"].ToString() != "")
        {
            QTree t = this.Session["tree"] as QTree;
            string node = this.Request.QueryString["node"].ToString().Trim();
            //string message = "";
            decimal temp;
            if (this.TextBox1.Visible)
            {
                if (decimal.TryParse(this.TextBox1.Text, out temp))
                {
                    try
                    {
                        MathNode n = t.GetNodeFromPath(node) as MathNode;
                        if (!n.hidden) n.formula = this.TextBox1.Text;
                        //message = "\"" + this.Label3.Text + "\"" + " value updated.";
                    }
                    catch (Exception)
                    {
                        //message = "Error entering the new value.";
                    }
                    finally
                    {
                        //this.Response.Redirect("success.aspx?message="+message);

                    }
                }
            }
            else
                if (this.Panel1.Visible)
                {
                    foreach (Control text in Panel1.Controls)
                    {
                        if (text is TextBox && decimal.TryParse((text as TextBox).Text, out temp))
                        {
                            MathNode n = t.GetNodeFromPath(node + "\\" + text.ID) as MathNode;
                            n.formula = (text as TextBox).Text;
                        }
                    }
                   // message = "Values updated.";

                }

            //Set parent's window "Total" label
            string script = "var xhReq1 = createXMLHttpRequest(); xhReq1.open(\"GET\", \"setNodeAttributes.aspx\", false); xhReq1.send(null); var serverResponse1 = xhReq1.responseText; this.parent.document.getElementById(\"ctl00_ContentPlaceHolder1_Label1\").innerText = \"Total = $\" + serverResponse1;";
            script += "document.location = parent.document.getElementById('ctl00_ContentPlaceHolder1_HyperLink6').href;";
            ClientScript.RegisterClientScriptBlock(this.GetType(), "showtotal", script, true);

            //this.Session["leftframe"] = "success.aspx?message=" + message;
        }


    }
}
